/**
 * 游戏模型
 * 处理游戏数据的存储、验证和操作
 */

const mongoose = require('mongoose');

const gameSchema = new mongoose.Schema({
    title: {
        type: String,
        required: [true, '游戏标题是必填项'],
        trim: true,
        minlength: [2, '游戏标题至少需要2个字符'],
        maxlength: [50, '游戏标题不能超过50个字符']
    },
    description: {
        type: String,
        required: [true, '游戏描述是必填项'],
        trim: true,
        minlength: [10, '游戏描述至少需要10个字符'],
        maxlength: [1000, '游戏描述不能超过1000个字符']
    },
    category: {
        type: String,
        required: [true, '游戏分类是必填项'],
        enum: {
            values: ['casual', 'action', 'puzzle', 'strategy', 'adventure'],
            message: '游戏分类必须是休闲、动作、益智、策略或冒险其中之一'
        }
    },
    filePath: {
        type: String,
        required: [true, '游戏文件路径是必填项']
    },
    fileType: {
        type: String,
        required: [true, '游戏文件类型是必填项']
    },
    fileSize: {
        type: Number,
        required: [true, '游戏文件大小是必填项']
    },
    thumbnail: {
        type: String,
        required: [true, '游戏缩略图是必填项']
    },
    author: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: [true, '游戏作者是必填项']
    },
    status: {
        type: String,
        enum: ['pending', 'approved', 'rejected'],
        default: 'pending'
    },
    plays: {
        type: Number,
        default: 0
    },
    downloads: {
        type: Number,
        default: 0
    },
    rating: {
        type: Number,
        min: 0,
        max: 5,
        default: 0
    },
    totalRatings: {
        type: Number,
        default: 0
    },
    featuredOrder: {
        type: Number,
        default: 0 // 0表示不推荐，数字越大表示推荐度越高
    },
    tags: [{
        type: String,
        trim: true
    }],
    createdAt: {
        type: Date,
        default: Date.now
    },
    updatedAt: {
        type: Date,
        default: Date.now
    }
});

// 创建索引以提高搜索性能
gameSchema.index({ title: 'text', description: 'text', tags: 'text' });
gameSchema.index({ category: 1 });
gameSchema.index({ author: 1 });
gameSchema.index({ createdAt: -1 });
gameSchema.index({ plays: -1 });
gameSchema.index({ downloads: -1 });
gameSchema.index({ rating: -1 });
gameSchema.index({ featuredOrder: -1 });

// 更新时间中间件
gameSchema.pre('save', function(next) {
    if (!this.isNew) {
        this.updatedAt = Date.now();
    }
    next();
});

// 获取热门游戏的静态方法
gameSchema.statics.getHotGames = function(limit = 10) {
    return this.find({ status: 'approved' })
        .sort({ plays: -1, downloads: -1, rating: -1 })
        .limit(limit)
        .populate('author', 'username avatar');
};

// 获取最新游戏的静态方法
gameSchema.statics.getNewGames = function(limit = 10) {
    return this.find({ status: 'approved' })
        .sort({ createdAt: -1 })
        .limit(limit)
        .populate('author', 'username avatar');
};

// 获取推荐游戏的静态方法
gameSchema.statics.getFeaturedGames = function(limit = 10) {
    return this.find({ status: 'approved', featuredOrder: { $gt: 0 } })
        .sort({ featuredOrder: -1 })
        .limit(limit)
        .populate('author', 'username avatar');
};

// 按分类获取游戏的静态方法
gameSchema.statics.getGamesByCategory = function(category, limit = 10, page = 1) {
    const skip = (page - 1) * limit;
    
    return this.find({ status: 'approved', category })
        .sort({ createdAt: -1 })
        .skip(skip)
        .limit(limit)
        .populate('author', 'username avatar');
};

// 搜索游戏的静态方法
gameSchema.statics.searchGames = function(query, limit = 10, page = 1) {
    const skip = (page - 1) * limit;
    
    return this.find(
        { 
            status: 'approved',
            $text: { $search: query } 
        },
        { 
            score: { $meta: "textScore" } 
        }
    )
    .sort({ score: { $meta: "textScore" } })
    .skip(skip)
    .limit(limit)
    .populate('author', 'username avatar');
};

const Game = mongoose.model('Game', gameSchema);

module.exports = Game; 